package com.devin.controller;

import com.devin.pojo.LogInfo;
import com.devin.service.LogService;
import com.github.pagehelper.PageInfo;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/logs")
public class LogController {
    @Autowired
    private LogService logService;

    @RequestMapping("/getLogs")
    @PreAuthorize("hasRole('ROLE_ADMIN')")    //管理员才能查看
    public ModelAndView getLogs(@RequestParam(value = "page",defaultValue = "1")Integer page
            ,@RequestParam(value = "size",defaultValue = "5")Integer size){
        List<LogInfo> logs = logService.getLogs(page, size);
        PageInfo<LogInfo> logInfoPageInfo = new PageInfo<>(logs);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("logInfoPageInfo",logInfoPageInfo);
        modelAndView.setViewName("pages/log-pages-list");
        return modelAndView;
    }

    @RequestMapping(value = "/delLog",method = RequestMethod.GET)
    public String delLog(Integer logId){
        logService.delLog(logId);
        return "redirect:/logs/getLogs";
    }
    @RequestMapping(value = "/delLogsChecked",method = RequestMethod.POST)
    public String delLogsChecked(Integer[] ids){
        logService.delLogsChecked(ids);
        return "redirect:/logs/getLogs";
    }
}
